package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.QServiceLevel;
import com.neusoft.neuiotms.dcom.model.ServiceLevel;
import com.neusoft.neuiotms.dcom.repository.CustomizedServiceLevelRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedServiceLevelRepositoryImpl extends AbstractRepository
        implements CustomizedServiceLevelRepository {

    @Override
    public List<ServiceLevel> search(final Searchable searchable) {
        final QServiceLevel q = QServiceLevel.serviceLevel;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<ServiceLevel> query = this.query().selectFrom(q);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<ServiceLevel> search(final Searchable searchable, final Pageable pageable) {
        final QServiceLevel q = QServiceLevel.serviceLevel;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<ServiceLevel> query = this.query().selectFrom(q);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QServiceLevel q = QServiceLevel.serviceLevel;

        if (!StringUtils.isEmpty(searchable.getStrValue("title"))) {
            where.and(super.containsStr(q.title, searchable, "title"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("id"))) {
            where.and(super.equalsLong(q.id, searchable, "id"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("keyword"))) {
            where.and(super.containsStr(q.keywords, searchable, "keyword"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("startTimeBegin"))) {
            where.and(super.afterOrEqualsDate(q.createTime, searchable, "startTimeBegin"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("startTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(q.createTime, searchable, "startTimeEnd"));
        }

        //全局
        if (!StringUtils.isEmpty(searchable.getStrValue("globleSearch"))) {
            final BooleanBuilder whereGlobal = new BooleanBuilder();
            whereGlobal.or(super.containsStr(q.title, searchable, "globleSearch"));
            whereGlobal.or(super.containsStr(q.content, searchable, "globleSearch"));
            whereGlobal.or(super.containsStr(q.userName, searchable, "globleSearch"));
            where.and(whereGlobal);
        }

        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return ServiceLevel.class;
    }

}
